package Concurrency1.Chapter01_Base;

import java.util.concurrent.*;

/**
 * 功能说明：创建线程的方式
 * 方式1: 继承Thread
 * 方式2: 实现Runnable接口
 * 方式3: 实现Callable接口
 * 开发人员：@author MaLi
 */
public class C01_CreateThread {
    //Mode1: extends Thread
    static class SomeThread extends Thread {
        @Override
        public void run() {
            System.out.println("The way of thread creation - extends Thread");
        }
    }

    /**
     * Mode2: implements Runnable
     */
    static class SomeRunnable implements Runnable {
        @Override
        public void run() {
            System.out.println("The way of thread creation - implement Runnable");
        }
    }

    /**
     * Mode3: implements Callable
     */
    static class SomeCallable implements Callable<String> {
        @Override
        public String call() throws Exception {
            System.out.println("The way of thread creation - implement Runnable");
            return "hello callable";
        }
    }

    public static void main(String[] args) {
        // Step1: create Thread
        Thread t1 = new Thread(new SomeThread());
        Thread t2 = new Thread(new SomeRunnable());
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        // Step2: run Thread
        t1.start();
        t2.start();
        Future<?> future = executorService.submit(new SomeThread());
        try {
            System.out.println("Mode3 's return value: " + future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
